package com.orange.common.base.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件发送服务
 */
@Component
@Slf4j
public class MailService {


    @Value("${spring.mail.username}")
    private String userName;

    @Value("${spring.mail.nickname}")
    private String nickname;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    TemplateEngine templateEngine;

    /**
     * 发送普通文本邮件
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String[] to ,String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        //发信人
        message.setFrom(nickname + "<" + userName + ">");
        //收信人
        message.setTo(to);
        //主题
        message.setSubject(subject);
        //文本
        message.setText(content);
        mailSender.send(message);
        log.info("发送邮件成功：发件人：{},收件人：{},主题：{}",userName, to, subject);
    }

    /**
     * 发送html邮件
     * @param to 收件人
     * @param subject 主题
     * @param context 内容
     * @param htmlTemplate 模板
     */
    public void sendHtmlMail(String[] to, String subject, Context context,String htmlTemplate) {
        //使用MimeMessage，MIME协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(nickname + "<" + userName + ">");
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            String content = templateEngine.process(htmlTemplate,context);
            messageHelper.setText(content, true);
            mailSender.send(message);
            log.info("发送HTML邮件成功：发件人：{},收件人：{},主题：{}",userName, to, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送带附件邮件
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     * @param filePath 附件地址
     */
    public void sendAttachmentMail(String[] to, String[] cc,String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            //true代表支持多组件，如附件，图片等
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(nickname + "<" + userName + ">");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            //添加附件，可多次调用该方法添加多个附件
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            log.info("发送带附件邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",userName, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送带附件邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",userName, to,cc, subject);
        }
    }

    /**
     * 发送带图片邮件
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     * @param rscPath 图片路径
     * @param rscId 图片id
     */
    public void sendInlineResourceMail(String[] to, String[] cc,String subject, String content, String rscPath, String rscId) {

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(nickname + "<" + userName + ">");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            //重复使用添加多个图片
            helper.addInline(rscId, res);
            mailSender.send(message);
            log.info("发送带图片邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",userName, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送带图片邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",userName, to,cc, subject);
        }
    }

}
